﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Noris.Tools.TraceVisualiser.Forms
{
    public partial class ShowTextForm : Noris.Tools.TraceVisualiser.Components.DForm
    {
        public ShowTextForm()
        {
            InitializeComponent();
            this._InitData();
        }
        /// <summary>
        /// 
        /// </summary>
        private void _InitData()
        {
            this.CancelButton = this._CloseBtn;
            this._FindTextPanel.Visible = this._FindPanelVisible;
            this._ArrangeItems();
        }
		#region Property a jejich obsluha
        /// <summary>
        /// Zobrazovaný text v čitelné podobě
        /// </summary>
        [Category("Data")]
		[Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string CurrentText
        {
            get { return this._TextGet(); }
            set { this._TextSet(value); }
        }
		/// <summary>
		/// Zobrazovaný text v RTF podobě
		/// </summary>
		[Category("Data")]
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public string CurrentRtfText
		{
			get { return this._TextRtfGet(); }
			set { this._TextRtfSet(value); }
		}
        /// <summary>
        /// Příznak, zda má být zobrazován panel FIND (default = false = ne)
        /// </summary>
        [Category("Data")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Příznak, zda má být zobrazován panel FIND (default = false = ne)")]
        public bool FindPanelVisible
        {
            get { return this._FindPanelVisible; }
            set { this._FindPanelVisibleSet(value); }
        }
        /// <summary>
        /// Příznak, že zadaný text je ReadOnly (true = readonly, nelze editovat)
        /// </summary>
        [Category("Data")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Příznak, že zadaný text je ReadOnly (true = readonly, nelze editovat)")]
        public bool ReadOnly
        {
            get { return this._ReadOnly; }
            set { this._ReadOnlySet(value); }
        }
        private bool _ReadOnly;
        #region Invoke pro přístup k property
        private void _TextSet(string value)
        {
            if (this.InvokeRequired)
                this.BeginInvoke(new Action<string>(this._TextSet), value);
            else
                this._DataText.Text = value;
        }
        private string _TextGet()
        {
            if (this.InvokeRequired)
                return (string)this.Invoke(new Func<string>(this._TextGet));
            else
                return this._DataText.Text;
        }
		private void _TextRtfSet(string value)
		{
			if (this.InvokeRequired)
				this.BeginInvoke(new Action<string>(this._TextRtfSet), value);
			else
				this._DataText.Rtf = value;
		}
		private string _TextRtfGet()
		{
			if (this.InvokeRequired)
				return (string)this.Invoke(new Func<string>(this._TextRtfGet));
			else
				return this._DataText.Rtf;
		}
        private void _ReadOnlySet(bool value)
		{
			if (this.InvokeRequired)
                this.BeginInvoke(new Action<bool>(this._ReadOnlySet), value);
			else
				this._DataText.ReadOnly = value;
		}
        #endregion
        #region Viditelnost panelu Find
        /// <summary>
        /// Nastaví viditelnost panelu Find
        /// </summary>
        /// <param name="value"></param>
        private void _FindPanelVisibleSet(bool value)
        {
            if (value != this._FindTextPanel.Visible)
            {
                if (this.InvokeRequired)
                    this.BeginInvoke(new Action<bool>(_FindPanelVisibleSetT), value);
                else
                    this._FindPanelVisibleSetT(value);
            }
            this._FindPanelVisible = value;
        }

        private void _FindPanelVisibleSetT(bool value)
        {
            this._FindPanelVisible = value;
            this._FindTextPanel.Visible = value;
            this._ArrangeItems();
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            this._ArrangeItems();
            base.OnSizeChanged(e);
        }
        /// <summary>
        /// Uspořádá controly formuláře s ohledem na jeho aktuální velikost a viditelnost controlu _FindTextPanel
        /// </summary>
        private void _ArrangeItems()
        {
			if (this._FindTextPanel == null || this._CloseBtn == null)
				return;

            Size innSize = this.ClientSize;
            bool fpv = this._FindPanelVisible;
            int xm = 8;
            int ym = 6;
            int ys = 4;
            int top = ym;
            int w;
            if (fpv)
            {
                w = innSize.Width - 48;
                if (w < 100) w = 100;
                if (w > 1000) w = 1000;
                this._FindTextPanel.Bounds = new Rectangle(xm, top, w, this._FindTextPanel.Height);
                top = this._FindTextPanel.Bottom + ys;
            }

            this._CloseBtn.Location = new Point(innSize.Width - this._CloseBtn.Width - xm, innSize.Height - this._CloseBtn.Height - ym);
            this._DataText.Bounds = new Rectangle(xm, top, innSize.Width - xm - xm, this._CloseBtn.Top - top - ys);
        }
        private bool _FindPanelVisible;
        #endregion
        #endregion
        #region Find
        private void _FindTextPanel_FindText(object sender, FindTextEventArgs args)
		{
			int oldStart = this._DataText.SelectionStart;
			int selStart = -1;
			string text = this._DataText.Text.ToLower();
			string find = args.FindText.ToLower();
			switch (args.FindMode)
			{
				case FindTextMode.FindFirst:
					selStart = text.IndexOf(find);
					break;
				case FindTextMode.FindNext:
					selStart = text.IndexOf(find, oldStart + 1);
					break;
				case FindTextMode.FindPrev:
					selStart = text.LastIndexOf(find, oldStart - 1);
					break;
				case FindTextMode.FindLast:
					selStart = text.LastIndexOf(find);
					break;
			}
			if (selStart >= 0)
			{
				this._DataText.SelectionStart = selStart;
				this._DataText.SelectionLength = find.Length;
			}
		}
		void _FindTextPanel_FocusToText(object sender, System.EventArgs e)
		{
			this._DataText.Focus();
		}
		#endregion
	}
}
